const formatTime = (date, symbol1, symbol2) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime_1 = (date, symbol1, symbol2) => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()

  return [month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute].map(formatNumber).join(symbol2 || ':')
}

const formatTime_2 = (date, symbol1, symbol2) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join(symbol1 || '/');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const dealObjectValue =(obj)=>{
    let param = {};
    console.log(obj)
    if ( obj === null || obj === undefined || obj === "" ) return param;
    for ( let key in obj ){
        if(typeof(obj[key]) === "Object" ){
            param[key] = dealObjectValue(obj[key]);
        }else if(  obj[key] !== null && obj[key] !== undefined && obj[key] !== ""  ){
            param[key] = obj[key];
        }
    }
    return param;
}

const testidCard = n => {
  if ((/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(n))){
    return false;
  }else{
    return true;
  }
}
const formatDate = date => {
  console.log(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
}
const IdCard=(UUserCard, num)=>{
  if (num == 1) {    //获取出生日期    
    var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);    
    return birth; 
  }  
  if (num == 2) {    //获取性别    
    if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {      //男      
      return "男";    
    } else {      //女      
      return "女";    
    }
  }  
  if (num == 3) {    //获取年龄    
    var myDate = new Date();    
    var month = myDate.getMonth() + 1;    
    var day = myDate.getDate();    
    var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;    
    if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) { 
      age++;
    }
    return age;
  }
}
const checkPhone = phone => {
  const reg = /^1(3|4|5|7|8|9|6)\d{9}$/;
  return reg.test(phone);
}


module.exports = {
  formatTime: formatTime,
  formatTime_1: formatTime_1,
  formatTime_2,
  dealObjectValue,
  testidCard,
  formatDate,
  IdCard,
  checkPhone
}